package com.b2b.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.SystemRouteAuthorityDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.SystemRouteAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/systemRouteAuthority")
@PropertySource("classpath:config.properties")
public class SystemRouteAuthorityController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final SystemRouteAuthorityService systemRouteAuthorityService;

    @Autowired
    public SystemRouteAuthorityController(SystemRouteAuthorityService systemRouteAuthorityService){
        this.systemRouteAuthorityService = systemRouteAuthorityService;
    }
    /**
     * 获取路由列表
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:41
     */
    @GetMapping
//    @RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean getAll(@Validated BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<SystemRouteAuthorityDto> systemRouteAuthorityDtos = systemRouteAuthorityService.selectAll();
        PageInfo<SystemRouteAuthorityDto> selectPage = new PageInfo<SystemRouteAuthorityDto>(systemRouteAuthorityDtos);
        if (systemRouteAuthorityDtos == null || systemRouteAuthorityDtos.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


    /**
     * 获取指定路由
     * @param parentId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/get")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(@RequestParam Integer parentId) {
        SystemRouteAuthorityDto systemRouteAuthorityDto = new SystemRouteAuthorityDto();
        systemRouteAuthorityDto.setParentId(parentId);
        List<SystemRouteAuthorityDto> systemRouteAuthorityDtoList = systemRouteAuthorityService.select(systemRouteAuthorityDto);

        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", systemRouteAuthorityDtoList);
    }

    /**
     * 新增路由
     * @param systemRouteAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PostMapping("/add")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add( @RequestBody SystemRouteAuthorityDto systemRouteAuthorityDto) {
        // 判断当前帐号是否存在
        SystemRouteAuthorityDto systemRouteAuthorityDtoTemp = new SystemRouteAuthorityDto();
        systemRouteAuthorityDtoTemp.setRouteName(systemRouteAuthorityDto.getRouteName());
        systemRouteAuthorityDtoTemp = systemRouteAuthorityService.selectOne(systemRouteAuthorityDtoTemp);
        if (systemRouteAuthorityDtoTemp != null ) {
            throw new CustomUnauthorizedException("该企业路由已存在(Account exist.)");
        }
        int count = systemRouteAuthorityService.insert(systemRouteAuthorityDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", systemRouteAuthorityDto);
    }


    /**
     * 更新路由
     * @param systemRouteAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody SystemRouteAuthorityDto systemRouteAuthorityDto) {
        // 查询数据库密码
        SystemRouteAuthorityDto systemRouteAuthorityDtoTemp = new SystemRouteAuthorityDto();
        systemRouteAuthorityDtoTemp.setRouteId(systemRouteAuthorityDto.getRouteId());
        systemRouteAuthorityDtoTemp = systemRouteAuthorityService.selectOne(systemRouteAuthorityDtoTemp);
        if (systemRouteAuthorityDto == null) {
            throw new CustomUnauthorizedException("该帐号不存在(Account not exist.)");
        }
        int count = systemRouteAuthorityService.updateByPrimaryKeySelective(systemRouteAuthorityDto);
        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", systemRouteAuthorityDto);
    }

    /**
     * 删除路由
     * @param systemRouteAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/byId")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestBody SystemRouteAuthorityDto systemRouteAuthorityDto) {
        SystemRouteAuthorityDto systemRouteAuthorityDtoTemp = new SystemRouteAuthorityDto();
        systemRouteAuthorityDtoTemp.setRouteName(systemRouteAuthorityDto.getRouteName());
        systemRouteAuthorityDtoTemp = systemRouteAuthorityService.selectOne(systemRouteAuthorityDtoTemp);
        if (systemRouteAuthorityDtoTemp == null ) {
            throw new CustomUnauthorizedException("该路由不存在(Account not exist.)");
        }
        int count = systemRouteAuthorityService.deleteByPrimaryKey(systemRouteAuthorityDto.getRouteId());
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
